/*
 * jxSwing Project (2012, 2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.text;

import java.awt.Dimension;
import java.security.InvalidParameterException;
import java.sql.Time;
import java.text.ParseException;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;

import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;
import org.jxUtils.date.DateUtil;
import org.jxUtils.date.HourFormatters;
import org.jxUtils.date.TimeFormatter;

/**
 * @author Denilson Edinaldo Pinto
 * @project jxSwing
 * @package org.jxSwing.component.text
 * @date 05/09/2012 - 21:31:22
 */
public final class HourField extends FormattedField {
	
	private static final long serialVersionUID = -428135339873517785L;
	
	private static final HourFormatters FORMATTERS = new HourFormatters();
	
	private TimeFormatter hourFormatter = new TimeFormatter();
	
	/**
     * 
     */
	public HourField() {
	
		this(false);
	}
	
	/**
	 * @param required
	 */
	public HourField(final boolean required) {
	
		this("##:##:##", required);
	}
	
	/**
	 * @param mask
	 */
	public HourField(final String mask) {
	
		this(mask, false);
	}
	
	/**
	 * @param mask
	 * @param required
	 */
	public HourField(final String mask, final boolean required) {
	
		super(getMask(mask), required);
		
		hourFormatter = FORMATTERS.getFormatterByMask(mask);
		
		initGUI();
	}
	
	/**
	 * @param mask
	 * @return {@link MaskFormatter} or null if {@link HourFormatters#containsMask(mask )} is false
	 */
	private static MaskFormatter getMask(String mask) {
	
		//
		if(FORMATTERS.containsMask(mask)) {
			
			try {
				//
				MaskFormatter maskFormatter = new MaskFormatter(mask);
				maskFormatter.setPlaceholderCharacter('_');
				
				return maskFormatter;
				
			}catch(final ParseException parseException) {
				
				parseException.printStackTrace();
			}
		}
		
		throw new InvalidParameterException(I18N.component().maskUnknown(mask));
	}
	
	/**
	 * @return {@link Time} or null
	 */
	private Time generateHour() {
	
		//
		if(isEmpty()) {
			
			return null;
			
		}else {
			
			try {
				// 
				return DateUtil.parseTime(getText());
				
			}catch(final ParseException parseException) {
				
				parseException.printStackTrace();
				
				return null;
			}
		}
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setName(I18N.component().hour());
		setSize(new Dimension(70, 25));
		setPreferredSize(new Dimension(70, 25));
		setHorizontalAlignment(CENTER);
		
		//
		setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(final JComponent input) {
			
				//
				if(isEmpty()) {
					
					setValue(null);
					
					return true;
					
				}else {
					
					try {
						//
						DateUtil.parse(getText());
						
						return true;
						
					}catch(final ParseException parseException) {
						
						JOptionPane.showMessageDialog(HourField.this, I18N.component().invalidValueHour(getText()));
						
						setValue(null);
						requestFocusInWindow();
					}
					
					return false;
				}
			}
		});
	}
	
	/**
	 * @return the hourFormatter
	 */
	public TimeFormatter getHourFormatter() {
	
		return hourFormatter;
	}
	
	/**
	 * @return the maskFormatter
	 */
	public MaskFormatter getMaskFormatter() {
	
		return getHourFormatter().getMaskFormatter();
	}
	
	/**
	 * @return the pattern
	 */
	public String getPattern() {
	
		return getHourFormatter().getPattern();
	}
	
	/**
	 * @return {@link Time} or null
	 */
	public Time getTime() {
	
		return getValue();
	}
	
	/**
	 * @return {@link Time} or null
	 */
	@Override
	public Time getValue() {
	
		return generateHour();
	}
	
	/**
	 * @return {@code getText().replaceAll("\\D", "").trim().isEmpty()}
	 */
	public boolean isEmpty() {
	
		return getText().replaceAll("\\D", "").trim().isEmpty();
	}
	
	/**
	 * @param hour
	 */
	public void setHour(final Time hour) {
	
		setText(hour != null ? hour.toString() : null);
		
		updateUI();
	}
	
	/**
	 * @param hour
	 * @throws ParseException 
	 */
	public void setHourText(final String hour) throws ParseException {
	
		// 
		if(CheckUtil.isNullOrAbsoluteEmpty(hour)) {
			
			setHour(null);
			
		}else {
			
			setHour(DateUtil.parseTime(hour));
		}
	}
	
	/**
	 * 
	 */
	public void setNow() {
	
		setHour(DateUtil.nowTime());
	}
}
